﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CarLoanSystem.Entity;
using System.Data;
using CarLoanSystem.DAL;
using CarLoanSystem.Web.Comman;

namespace CarLoanSystemWeb.Page.Estimator
{
    public partial class wuc_estmator : UserControlManageForm
    {
        private static CustomerItem cusItem;
        private static CarInfoItem carItem;
        private static InterrestInfoItem InterrestItem;
        private static decimal downPay;
        private static decimal totalAmount;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cusItem = Session["Member"] as CustomerItem;
                ddlBrand.AppendDataBoundItems = true;
                ddlDownPay.AppendDataBoundItems = true;
                ddlEngine.AppendDataBoundItems = true;
                ddlModel.AppendDataBoundItems = true;
                ddlMonth.AppendDataBoundItems = true;
            }
        }

        protected void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataSource2.DataBind();
            SqlDataSource3.DataBind();
        }

        protected void ddlModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataSource3.DataBind();
        }

        protected void ddlEngine_SelectedIndexChanged(object sender, EventArgs e)
        {
            carItem = CarInfoDAL.LoadCar(Convert.ToInt32(this.ddlEngine.SelectedValue.ToString()));
            this.txtPrice.Text = carItem.CAR_PRICE.ToString();
        }

        protected void ddlDownPay_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataSource5.DataBind();
            if (!this.txtPrice.Text.Equals(string.Empty))
            {
                downPay = Convert.ToDecimal(this.txtPrice.Text) * (Convert.ToDecimal(ddlDownPay.SelectedItem.Text) / 100);
                this.txtPay.Text = downPay.ToString();
            }
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            InterrestItem = InterrestInfoDAL.LoadInterrest(Convert.ToInt32(this.ddlMonth.SelectedValue.ToString()));
            this.txtInterest.Text = InterrestItem.INTERREST_RATE.ToString();
            totalAmount = (((carItem.CAR_PRICE - downPay) * InterrestItem.INTERREST_RATE) / 100) + (carItem.CAR_PRICE - downPay);
            this.txtPriceInterest.Text = decimal.Round(totalAmount, 2, MidpointRounding.AwayFromZero).ToString();
            this.txtPayMonth.Text = decimal.Round((totalAmount / InterrestItem.DEBT_MONTH)).ToString();
        }
    }
}